//
//  MKWelcomeViewController.swift
//  MKWeiBo2017
//
//  Created by nicolas on 2017/8/22.
//  Copyright © 2017年 nicolas. All rights reserved.
//

import UIKit
import SnapKit
import SDWebImage

class MKWelcomeViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        
        prepareUI()
        setIcon()
        // 获取用户信息
        MKUserAccountViewModel.sharedInstance.loadUserInfo { () -> () in
            self.setIcon()
        }
    }
    
    override func viewDidAppear(_ animated: Bool) {
        super.viewDidAppear(animated)
        moveAnimation()
    }
    
    private func setIcon() {
        if let avatar_large = MKUserAccountViewModel.sharedInstance.userAccount?.avatar_large {
            // 获取到了用户数据,设置头像
            let url = NSURL(string: avatar_large)!
            
            self.iconView.sd_setImage(with: url as URL, placeholderImage: UIImage(named: "avatar_default_big"))
        }
    }
    // MARK: - 动画
    private func moveAnimation() {
        
        UIView .animate(withDuration: 1, animations: {
            
            self.iconView.snp.updateConstraints({ (make) in
                make.bottom.equalTo(self.view).offset(-(UIScreen.main.bounds.height-160))
            })
            self.view.layoutIfNeeded()
            
        }) { (_) in
            
            UIView.animate(withDuration: 1, animations: { () -> Void in
                self.messageLabel.alpha = 1
                }, completion: { (_) -> Void in
                    // 切换到MainViewController
                    let appDelegate = (UIApplication.shared.delegate as! AppDelegate)
                    appDelegate.switchRootViewController(controller: MKMainViewController())
            })

        }
    }
    
    private func prepareUI() {
        
        view.addSubview(bkgView)
        view.addSubview(iconView)
        view.addSubview(messageLabel)
        //背景
        bkgView.snp.makeConstraints { (make) in
            make.edges.equalTo(self.view)
        }
        //头像
        iconView.snp.makeConstraints { (make) in
            make.centerX.equalTo(self.view.snp.centerX)
            make.bottom.equalTo(self.view.snp.bottom).offset(-160)
            make.size.equalTo(CGSize(width: 85, height: 85))
        }
        //消息
        messageLabel.snp.makeConstraints { (make) in
            make.centerX.equalTo(iconView)
            make.top.equalTo(iconView.snp.bottom).offset(16)
        }
        
    }
    
    //MARK: - 懒加载
    //背景
    private lazy var bkgView: UIImageView = UIImageView(image: UIImage(named: "ad_background"))
    //头像
    private lazy var iconView: UIImageView = {
        
        let imageView = UIImageView(image: UIImage(named: "avatar_default_big"))
        
        imageView.layer.cornerRadius = 42.5
        imageView.layer.masksToBounds = true
        
        return imageView;
    }()
    //消息
    private lazy var messageLabel: UILabel = {
        
        let label = UILabel()
        label.text = "欢迎归来"
        label.textColor = UIColor.black
        label.font = UIFont.systemFont(ofSize: 15)
        label.sizeToFit()
        return label
        
    }()
}
